package com.hospital.service.impl;

import com.hospital.dao.GradeDao;
import com.hospital.entity.Grade;
import com.hospital.service.intf.GradeService;
import com.hospital.util.StringUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeDao gradeDao;

    @Override
    public R add(Grade grade) {
        if(gradeDao.add(grade) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R delete(Integer id) {
        if(gradeDao.delete(id) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R update(Grade grade) {
        if(gradeDao.update(grade) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R select() {
        if(gradeDao.select() != null){
            return R.ok(gradeDao.select());
        }
        return R.fail();
    }

    @Override
    public PageBean selectPage(String grade_name, int page, int limit) {
        int start = (page - 1) * limit;

        if(StringUtil.isNotEmpty(grade_name)){
            grade_name = "%"+grade_name+"%";
        }

        return PageBean.ok(gradeDao.count(grade_name),gradeDao.selectPage(grade_name,start,limit));
    }
}
